home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / DABASE.PY < prev    next >
Encoding:
Python Source  |  1999-11-03  |  9.2 KB  |  264 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. __doc__='''Database Connection
  65.  
  66. $Id: DABase.py,v 1.9 1999/11/03 18:56:33 sroberts Exp $'''
  67. __version__='$Revision: 1.9 $'[11:-2]
  68.  
  69. from db import manage_DataSources
  70. import Shared.DC.ZRDB.Connection, sys
  71. from Globals import HTMLFile
  72. from ExtensionClass import Base
  73. import Acquisition
  74.  
  75. class Connection(Shared.DC.ZRDB.Connection.Connection):
  76.     _isAnSQLConnection=1
  77.  
  78.     manage_options=Shared.DC.ZRDB.Connection.Connection.manage_options+(
  79.         {'label': 'Browse', 'action':'manage_browse'},
  80.         # {'label': 'Design', 'action':'manage_tables'},
  81.         )
  82.  
  83.     manage_tables=HTMLFile('tables',globals())
  84.     manage_browse=HTMLFile('browse',globals())
  85.  
  86.     info=None
  87.         
  88.     def tpValues(self):
  89.         #if hasattr(self, '_v_tpValues'): return self._v_tpValues
  90.         r=[]
  91.         # self._v_tables=tables=TableBrowserCollection()
  92.         #tables=tables.__dict__
  93.         c=self._v_database_connection
  94.         try:
  95.             for d in c.tables(rdb=0):
  96.                 try:
  97.                     name=d['TABLE_NAME']
  98.                     b=TableBrowser()
  99.                     b.__name__=name
  100.                     b._d=d
  101.                     b._c=c
  102.                     # b._columns=c.columns(name)
  103.                     try: b.icon=table_icons[d['TABLE_TYPE']]
  104.                     except: pass
  105.                     r.append(b)
  106.                     # tables[name]=b
  107.                 except:
  108.                     # print d['TABLE_NAME'], sys.exc_type, sys.exc_value
  109.                     pass
  110.  
  111.         finally: pass #print sys.exc_type, sys.exc_value
  112.         #self._v_tpValues=r
  113.         return r
  114.  
  115.     def __getitem__(self, name):
  116.         if name=='tableNamed':
  117.             if not hasattr(self, '_v_tables'): self.tpValues()
  118.             return self._v_tables.__of__(self)
  119.         raise KeyError, name
  120.  
  121.     def manage_wizard(self, tables):
  122.         " "
  123.  
  124.     def manage_join(self, tables, select_cols, join_cols, REQUEST=None):
  125.         """Create an SQL join"""
  126.  
  127.     def manage_insert(self, table, cols, REQUEST=None):
  128.         """Create an SQL insert"""
  129.  
  130.     def manage_update(self, table, keys, cols, REQUEST=None):
  131.         """Create an SQL update"""
  132.  
  133. class TableBrowserCollection(Acquisition.Implicit):
  134.     "Helper class for accessing tables via URLs"
  135.  
  136. class Browser(Base):
  137.     def __getattr__(self, name):
  138.         try: return self._d[name]
  139.         except KeyError: raise AttributeError, name
  140.  
  141. class TableBrowser(Browser, Acquisition.Implicit):
  142.     icon='what'
  143.     Description=check=''
  144.     info=HTMLFile('table_info',globals())
  145.     menu=HTMLFile('table_menu',globals())
  146.  
  147.     def tpValues(self):
  148.         r=[]
  149.         tname=self.__name__
  150.         for d in self._c.columns(tname):
  151.             b=ColumnBrowser()
  152.             b._d=d
  153.             try: b.icon=field_icons[d['Type']]
  154.             except: pass
  155.             b.TABLE_NAME=tname
  156.             r.append(b)
  157.         return r
  158.             
  159.     def tpId(self): return self._d['TABLE_NAME']
  160.     def tpURL(self): return "Table/%s" % self._d['TABLE_NAME']
  161.     def Name(self): return self._d['TABLE_NAME']
  162.     def Type(self): return self._d['TABLE_TYPE']
  163.  
  164.     manage_designInput=HTMLFile('designInput',globals())
  165.     def manage_buildInput(self, id, source, default, REQUEST=None):
  166.         "Create a database method for an input form"
  167.         args=[]
  168.         values=[]
  169.         names=[]
  170.         columns=self._columns
  171.         for i in range(len(source)):
  172.             s=source[i]
  173.             if s=='Null': continue
  174.             c=columns[i]
  175.             d=default[i]
  176.             t=c['Type']
  177.             n=c['Name']
  178.             names.append(n)
  179.             if s=='Argument':
  180.                 values.append("<dtml-sql-value %s type=%s>'" %
  181.                               (n, vartype(t)))
  182.                 a='%s%s' % (n, boboType(t))
  183.                 if d: a="%s=%s" % (a,d)
  184.                 args.append(a)
  185.             elif s=='Property':
  186.                 values.append("<dtml-sql-value %s type=%s>'" %
  187.                               (n, vartype(t)))
  188.             else:
  189.                 if isStringType(t):
  190.                     if find(d,"\'") >= 0: d=join(split(d,"\'"),"''")
  191.                     values.append("'%s'" % d)
  192.                 elif d:
  193.                     values.append(str(d))
  194.                 else:
  195.                     raise ValueError, (
  196.                         'no default was given for <em>%s</em>' % n)
  197.  
  198.             
  199.             
  200.  
  201. class ColumnBrowser(Browser):
  202.     icon='field'
  203.  
  204.     def check(self):
  205.         return ('\t<input type=checkbox name="%s.%s">' %
  206.                 (self.TABLE_NAME, self._d['Name']))
  207.     def tpId(self): return self._d['Name']
  208.     def tpURL(self): return "Column/%s" % self._d['Name']
  209.     def Description(self):
  210.         d=self._d
  211.         if d['Scale']:
  212.             return " %(Type)s(%(Precision)s,%(Scale)s) %(Nullable)s" % d
  213.         else:
  214.             return " %(Type)s(%(Precision)s) %(Nullable)s" % d
  215.  
  216. table_icons={
  217.     'TABLE': 'table',
  218.     'VIEW':'view',
  219.     'SYSTEM_TABLE': 'stable',
  220.     }
  221.  
  222. field_icons={
  223.     'BIGINT': 'int',
  224.     'BINARY': 'bin',
  225.     'BIT': 'bin',
  226.     'CHAR': 'text',
  227.     'DATE': 'date',
  228.     'DECIMAL': 'float',
  229.     'DOUBLE': 'float',
  230.     'FLOAT': 'float',
  231.     'INTEGER': 'int',
  232.     'LONGVARBINARY': 'bin',
  233.     'LONGVARCHAR': 'text',
  234.     'NUMERIC': 'float',
  235.     'REAL': 'float',
  236.     'SMALLINT': 'int',
  237.     'TIME': 'time',
  238.     'TIMESTAMP': 'datetime',
  239.     'TINYINT': 'int',
  240.     'VARBINARY': 'bin',
  241.     'VARCHAR': 'text',
  242.     }
  243.